package com.hspedu.jdbc.utils;

import java.sql.*;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/4/9
 * @time 17:53
 */
public class TestJDBCUtils {

    public static void test() {
        Connection connection = null;
        String sql = "update actor set name = ? where id = ?";
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "周星驰");
            preparedStatement.setInt(2, 2);
            int affectRows = preparedStatement.executeUpdate();
            System.out.println("affectRows = " + affectRows);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, preparedStatement, connection);
        }
    }

    public static void test2() {
        Connection connection = null;
        String sql = "select * from actor";
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String sex = resultSet.getString(3);
                Date borndate = resultSet.getDate(4);
                String phone = resultSet.getString(5);
                System.out.println(id + " " + name + " " + sex + " " + borndate + " " + phone);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(resultSet, preparedStatement, connection);
        }
    }
}
